package org.playground.booking.dto;

import com.fasterxml.jackson.annotation.JsonFormat;
import jdk.nashorn.internal.runtime.regexp.joni.Regex;
import org.playground.booking.entity.Booking;
import org.playground.booking.entity.BookingStatus;
import org.playground.booking.enums.BookingStatusEnum;
import org.springframework.format.annotation.DateTimeFormat;

import javax.swing.text.DateFormatter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class BookingDto {

    private Long bookingId;

    @JsonFormat(pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date start;

    @JsonFormat(pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date end;

    private Long userId;

    private CoursDto cours;

    private BookingStatusEnum bookingStatus;

    private boolean includMinute =false;
    //08-09
    private String time;

    public Long getBookingId() {
        return bookingId;
    }

    public void setBookingId(Long bookingId) {
        this.bookingId = bookingId;
    }

    public BookingStatusEnum getBookingStatus() {
        return bookingStatus;
    }

    public void setBookingStatus(BookingStatusEnum bookingStatus) {
        this.bookingStatus = bookingStatus;
    }

    public CoursDto getCours() {
        return cours;
    }

    public void setCours(CoursDto cours) {
        this.cours = cours;
    }

    public BookingDto(){}

    public BookingDto(boolean includMinute){
        this.includMinute=includMinute;
    }

    public void setStart(Date start) {
        this.start = start;
    }

    public void setEnd(Date end) {
        this.end = end;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public void setTime(String time){
        this.time=time;
        String[] startAndEnd = time.split("-");
        Calendar newStart = Calendar.getInstance();
        if(start!=null) {
            newStart.setTime(start);
        }
        newStart.set(Calendar.HOUR_OF_DAY,Integer.parseInt(startAndEnd[0]));
        newStart.set(Calendar.MINUTE,0);
        newStart.set(Calendar.SECOND,0);

        Calendar newEnd = Calendar.getInstance();
        if(end!=null) {
            newEnd.setTime(end);
        }
        newEnd.set(Calendar.HOUR_OF_DAY,Integer.parseInt(startAndEnd[1]));
        newEnd.set(Calendar.MINUTE,0);
        newEnd.set(Calendar.SECOND,0);

        this.start=newStart.getTime();
        this.end=newEnd.getTime();
    }

    public String getTime() {
        DateFormat df;
        if(includMinute){
            df = new SimpleDateFormat("HH:mm");
        }else{
            df =new SimpleDateFormat("HH");
        }
        String start = df.format(this.start);
        String end = df.format(this.end);
        return start+"-"+end;
    }

    public Date getStart() {
        return start;
    }

    public Date getEnd() {
        return end;
    }
}
